package org.vectrics.recruiter.struts.tag;

import java.util.Iterator;

import org.apache.ecs.html.A;
import org.apache.ecs.html.IMG;
import org.vectrics.SystemException;
import org.vectrics.common.html.Cell;
import org.vectrics.common.html.Row;
import org.vectrics.common.html.Table;
import org.vectrics.common.html.TextHelper;
import org.vectrics.common.util.FileUtil;
import org.vectrics.common.util.StringUtils;
import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.user.resume.email.EmailedResumeAttachment;
import org.vectrics.recruiter.user.resume.email.EmailedResumeInfo;
import org.vectrics.struts.Request;
import org.vectrics.struts.VectricsTagSupport;


public class EmailedResumeAttachmentListTag extends VectricsTagSupport {
	public static final long serialVersionUID = 362957877;
	private String emailedResumeId = null;
	private String readOnly = "false";
	private String showEmailBody = "true";
	
	public EmailedResumeAttachmentListTag() {
		super();
	}

	/**
	 * @return Returns the readOnly.
	 */
	public String getReadOnly() {
		return readOnly;
	}

	/**
	 * @param readOnly The readOnly to set.
	 */
	public void setReadOnly(String readOnly) {
		this.readOnly = readOnly;
	}

	protected String makeStartTag() {
		if (emailedResumeId == null)
			throw new SystemException("'emailedResumeId' not set for tag");
		EmailedResumeInfo emailResumeInfo = RecruitServiceLocator.getRecruitUserService().findEmailedResume(new Long(this.emailedResumeId));
		if (emailResumeInfo == null)
			throw new SystemException("Object not found for id: " + this.emailedResumeId);
		Table attachmentTable = createAttachmentListTable(this.getRequest(), emailResumeInfo, (this.readOnly.equals("true"))
				, this.showEmailBody.equals("true"));
		return attachmentTable.toString();
	}
	
	public static Table createAttachmentListTable(Request request, EmailedResumeInfo emailResumeInfo, 
			boolean readOnly, boolean showEmailBody) {
		Table attachmentTable = new Table();
		attachmentTable.setWidth("100%");
		makeSepLine(attachmentTable, request.getContextPath());
		Iterator iter = emailResumeInfo.getAttachments().iterator();
		while (iter.hasNext()) {
			Row row = attachmentTable.addRow();
			row.setDefaultCellFontClass("stdFont");
			EmailedResumeAttachment attachment = (EmailedResumeAttachment)iter.next();
			//if ((attachment.getOriginalFileName() != null) && (attachment.getOriginalFileName().trim().length() > 0)) {
				A retrieveLink = new A();
	            retrieveLink.setClass("subLink");
	            retrieveLink.setHref(request.getContextPath() + "/retrievedocument/attachment/document." 
	            		+ attachment.getFileExtention() 
	            		+ "?documentId=" + attachment.getDocument().getId()
                		+ "&userName=" + ThreadProperties.getPerson().getUser().getUserName()
                		+ "&password=" + ThreadProperties.getPerson().getUser().getPassword()
                		+ "&domainId=" + ThreadProperties.getDomain().getId().toString()
            			);
	            retrieveLink.setOnClick("OpenLarge(this.href, '" + attachment.getId() + "'); return false");
	            if (StringUtils.isNullOrBlank(attachment.getDocument().getOriginalFileName())) {
	            	retrieveLink.addElement(FileUtil.extractFileNameFromPath("Email Body"));
	            } else {
	            	retrieveLink.addElement(FileUtil.extractFileNameFromPath(
	            			attachment.getDocument().getOriginalFileName()));
	            }
				Cell cell = row.addCell();
				cell.setClass("sectionBoxHdrBgMedium");
				String attachmentType = null;
				if (attachment.getIsEmailedBody()) {
					attachmentType = CoreServiceLocator.getPhraseProvider().getMessage("phrase.email.body", 
							request.getLocale());
				} else {
					attachmentType = CoreServiceLocator.getPhraseProvider().getMessage("phrase.email.attachment", 
							request.getLocale());
				}
				if (readOnly) {
					attachmentType += "<br>" + attachment.getDocument().getCategory().getDescription(request.getLocale());
				}
				
				cell.addText("sectionHdrFont", attachmentType);
				
				row = attachmentTable.addRow();
				cell = row.addCell();
				if ((attachment.getIsEmailedBody()) && (showEmailBody)) {
					String contentHtml = TextHelper.getHtmlParagraphs("subFont", attachment.getDocument().getTextContent());
					cell.addElement(contentHtml);
				} else {
					cell.addElement(retrieveLink);
				}
				cell.addElement("<br><br>");
				if (readOnly == false) {
					cell.addElement(new EmailedResumeAttachmentCategoryCombo(request.getLocale(), attachment));
				} 
				makeSepLine(attachmentTable, request.getContextPath());
		}
		return attachmentTable;
		
	}

	private static void makeSepLine(Table table, String contextPath) {
		Row row = table.addRow();
		Cell cell = row.addCell();
		cell.setColSpan(1);
		IMG sepLine = new IMG();
		sepLine.setWidth("100%");
		sepLine.setHeight(1);
		sepLine.setSrc(contextPath + "/graphics/dk_slate_dot.gif");
		cell.addElement(sepLine);
	}

	/**
	 * @return Returns the emailedResumeId.
	 */
	public String getEmailedResumeId() {
		return emailedResumeId;
	}

	/**
	 * @param emailedResumeId The emailedResumeId to set.
	 */
	public void setEmailedResumeId(String emailedResumeId) {
		this.emailedResumeId = emailedResumeId;
	}

}
